package com.pankajm.common;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class IoHelper {
	private static BufferedReader br;
	static {
		BufferedInputStream bis = new BufferedInputStream(System.in);
		InputStreamReader isr = new InputStreamReader(bis);
		br = new BufferedReader(isr);
	}

	public static String readLine() throws IOException {
		return br.readLine();
	}

	public static Integer readInt() throws IOException {
		do {
			try {
				String line = br.readLine();
				int n; 
				n = Integer.parseInt(line);
				return n;
			} catch (NumberFormatException e) {
				System.out.println("Please enter a valid integer again.");
			}	
		} while (true);
	}
	
	public static Integer readOptionalInt() throws IOException {
		String line = br.readLine();
		if (line.equalsIgnoreCase("")) {
			return null;
		} else {
			try {
				int n; 
				n = Integer.parseInt(line);
				return n;
			} catch (NumberFormatException e) {
				return null;
			}	
		}
		
	}
}
